package cn.jdy.plugins.service;

import java.util.HashMap;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Service;

import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.plugins.entity.RobotLinks;

@Service
public class RobotLinksService extends BaseDao<RobotLinks>{
	public int count(String name, String status, String fromDate, String toDate) {
		int num = 0;
		try {
			String sql = "SELECT COUNT(0) FROM robotlinks l,robotmanager m WHERE l.managerid=m.id";
			if (name != null && !"".equals(name) && status != null && !"".equals(status)) {
				 sql += " AND INSTR(m.name,?)>0";
				 sql += " AND l.status = ?";
			} else if (name != null && !"".equals(name)) {
				sql += " AND INSTR(m.name,?)>0";
			} else if (status != null && !"".equals(status)) {
				sql += " AND l.status = ?";
			}  
			sql += " ORDER BY l.createTime DESC";
			SQLQuery query = super.em().createSQLQuery(sql);
			if (name != null && !"".equals(name) && status != null && !"".equals(status)) {
				 query.setParameter(0, name);
				 query.setParameter(1, status);
			} else if (name != null && !"".equals(name)) {
				 query.setParameter(0, name);
			} else if (status != null && !"".equals(status)) {
				query.setParameter(0, status);
			} 
			
			num = Integer.parseInt(String.valueOf(query.uniqueResult()));
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询站点采集结果数量异常", e);
		}
		return num;
	}

	/**
   * 查询用户列表
   */
  public List<HashMap<String ,Object>> list(String name, String status, String fromDate, String toDate, int iDisplayStart, int iDisplayLength) {
    try {
    	String sql = "SELECT m.name,m.url,l.id,l.title,l.link,l.status,CASE WHEN l.status = '2' THEN '已发布' WHEN l.status = '1' THEN '已采集' ELSE '未采集' END AS statusname "
    			         + "FROM robotlinks l,robotmanager m WHERE l.managerid=m.id";
		if (name != null && !"".equals(name) && status != null && !"".equals(status)) {
			 sql += " AND INSTR(m.name,?)>0";
			 sql += " AND l.status = ?";
		} else if (name != null && !"".equals(name)) {
			sql += " AND INSTR(m.name,?)>0";
		} else if (status != null && !"".equals(status)) {
			sql += " AND l.status = ?";
		} 
		sql += " ORDER BY l.createTime DESC LIMIT ?, ?";
		
		SQLQuery query = super.em().createSQLQuery(sql);
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		
		if (name != null && !"".equals(name) && status != null && !"".equals(status)) {
			query.setParameter(0, name);
			query.setParameter(1, status);
			query.setParameter(2, iDisplayStart);
			query.setParameter(3, iDisplayLength);
		} else if (name != null && !"".equals(name)) {
			query.setParameter(0, name);
			query.setParameter(1, iDisplayStart);
			query.setParameter(2, iDisplayLength);
		} else if (status != null && !"".equals(status)) {
			query.setParameter(0, status);
			query.setParameter(1, iDisplayStart);
			query.setParameter(2, iDisplayLength);
		} else {
			query.setParameter(0, iDisplayStart);
			query.setParameter(1, iDisplayLength);
		}
       
      return  query.list();
      
    } catch(Exception e) {
      BaseLog.e(this.getClass(), "查询站点采集结果列表异常", e);
    }
    return null;
  }
}
